package org.example.examfx02.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import org.example.examfx02.entity.Student;
import org.example.examfx02.entity.Teacher;
import org.example.examfx02.tool.ReadFile;
import org.example.examfx02.tool.StringUtil;
import org.example.examfx02.HelloApplication;
import org.example.examfx02.tool.SystemData;

import java.util.List;

public class HelloController {
    public static SystemData sys = SystemData.getInstance();
    @FXML
    private TextField textId;
    @FXML
    private TextField textPassword;
    @FXML
    private Label warningText;
    @FXML
    private RadioButton adminChoice;
    @FXML
    private RadioButton studentChoice;
    @FXML
    private RadioButton teacherChoice;

    @FXML
    public void loginCheck(){
        String uid = textId.getText();
        String pwd = textPassword.getText();
        if(StringUtil.isEmpty(uid)){
            warningText.setText("请输入账户！！！");
            warningText.setVisible(true);
        }else if(StringUtil.isEmpty(pwd)){
            warningText.setText("请输入密码！！！");
            warningText.setVisible(true);
        }else{
            boolean judge = false;
            if(adminChoice.isSelected()){
                String id = "admin";
                String password = "123456";
                if(id.equals(uid)&&password.equals(pwd)){
                    warningText.setVisible(false);
                    judge = true;
                    // 进入界面
                    HelloApplication.changeView("main-view.fxml",800,600);
                }
            }else if(teacherChoice.isSelected()){
                List<Teacher> list = sys.getUserGroup().getTeachers();
                for(int i = 0;i<list.size();i++){
                    String id = list.get(i).getWorkId();
                    String password = list.get(i).getPassword();
                    if(id.equals(uid)&&password.equals(pwd)){
                        sys.setCurUser(list.get(i));
                        judge = true;
                        warningText.setVisible(false);
                        // 进入界面
                        HelloApplication.changeView("tea-view.fxml",800,600);
                    }
                }
            }else if(studentChoice.isSelected()){
                List<Student> list = sys.getUserGroup().getStudents();
                for(int i = 0;i<list.size();i++){
                    String id = list.get(i).getId();
                    String password = list.get(i).getPassword();
                    if(id.equals(uid)&&password.equals(pwd)){
                        sys.setCurUser(list.get(i));
                        warningText.setVisible(false);
                        judge = true;
                        // 进入界面
                        HelloApplication.changeView("stu-view.fxml",800,600);
                    }else{
                        warningText.setText("账号或密码不正确！");
                        warningText.setVisible(true);
                    }
                }
            }
            if(!judge){
                warningText.setText("账号或密码不正确！");
                warningText.setVisible(true);
            }
        }
    }
    public void initialize(){
        ToggleGroup group = new ToggleGroup();
        adminChoice.setToggleGroup(group);
        teacherChoice.setToggleGroup(group);
        studentChoice.setToggleGroup(group);
        sys.setCourses(ReadFile.readCourseFile());
        sys.getUserGroup().setTeachers(ReadFile.readTeaFile());
        sys.getUserGroup().setStudents(ReadFile.readStuFile());
        sys.getSc().setScList(ReadFile.readSCFile());
    }
}